<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="i in filterData.brands"
          :key="i.id"
          :class="{ active: filterData.brands.selectedBrand === i.id }"
          @click="changeBrand(i.id)"
          >{{ i.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="i in filterData.saleProperties" :key="i">
      <div class="head">{{ i.name }}：</div>
      <div class="body">
        <a
          href="javascript:;"
          @click="changeProps(i, props.id)"
          v-for="props in i.properties"
          :key="props.id"
          :class="{ active: i.properties.selectedProps === props.id }"
          >{{ props.name }}</a
        >
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <skeleton class="item" width="800px" height="40px" />
    <skeleton class="item" width="800px" height="40px" />
    <skeleton class="item" width="600px" height="40px" />
    <skeleton class="item" width="600px" height="40px" />
    <skeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { useRoute } from "vue-router";
import { ref, watch } from "vue";
import { findSubCategoryFilter } from "api/home";
export default {
  name: "SubFilter",
  setup(props, { emit }) {
    const route = useRoute();
    const filterData = ref(null);
    //设置flag，用来加载骨架，否则路由规则是不会刷新页面的
    const filterLoading = ref(null);
    watch(
      () => route.params.id,
      (newValue) => {
        filterLoading.value = true;
        if (newValue && `/category/sub/${newValue}` === route.path) {
          findSubCategoryFilter(route.params.id).then((data) => {
            //默认选中id,null对应全部
            data.result.brands.selectedBrand = null;
            //1、品牌
            data.result.brands.unshift({
              id: null,
              name: "全部",
            });
            data.result.saleProperties.forEach((item) => {
              item.properties.selectedProps = null;
              item.properties.unshift({
                id: null,
                name: "全部",
              });
            });
            filterData.value = data.result;
            filterLoading.value = false;
          });
        }
      },
      {
        immediate: true,
      }
    );
    //获取筛选数据
    const getFilterParams = () => {
      const obj = { brandID: null, attrs: [] };
      obj.brandID = filterData.value.brands.selectedBrand;
      filterData.value.saleProperties.forEach((item) => {
        if (item.properties.selectedProps) {
          const prop = item.properties.find((prop) => {
            return prop.id === item.properties.selectedProps;
          });
          obj.attrs.push({ groupName: item.name, propertyName: prop.name });
        }
      });
      //为空axios就不发请求
      if (obj.attrs.length === 0) obj.attrs = null;
      return obj;
    };
    //发送筛选请求
    const changeBrand = (id) => {
      if (filterData.value.selectedBrand === id) return;
      filterData.value.brands.selectedBrand = id;
      emit("changeSort", getFilterParams());
    };
    const changeProps = (item, id) => {
      if (item.properties.selectedProps === id) return;
      item.properties.selectedProps = id;
      // console.log(item.properties.selectedProps);
      emit("changeFilter", getFilterParams());
    };
    return {
      filterData,
      filterLoading,
      changeBrand,
      changeProps,
    };
  },
};
</script>
<style scoped lang='less'>
.xtx-skeleton {
  padding: 10px 0;
}

// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;

  .item {
    display: flex;
    line-height: 40px;

    .head {
      width: 80px;
      color: #999;
    }

    .body {
      flex: 1;

      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;

        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
